using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameLayer;
using GameCharacter.AutoCharacter;
using GameCharacter;
using GameInstrument;
using MainGame.GameForm;
using GameBase;
using MainGame.GameInstrument;
using System.Xml;

namespace MainGame
{
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Round Current;
        public static RoundInfo CurrentRoundInfo { get; private set; }
        List<RoundInfo> Rounds;
        public static string SettingPath = "GameSetting.xml";
        public int Index { get; set; }
        public FormGroup MainMenu { get; set; }
        public FormGroup HelpMenu { get; set; }
        public FormGroup GameOver { get; set; }
        public FormGroup PrevNextRound { get; set; }
        public FormGroup Winner { get; set; }
        public FormGroup PlayContinue { get; set; }
        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            Rounds = new List<RoundInfo>();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            graphics.PreferredBackBufferWidth = 1300;
            graphics.PreferredBackBufferHeight = 670;
//            graphics.IsFullScreen = true;
            graphics.ApplyChanges();
            spriteBatch = new SpriteBatch(GraphicsDevice);

            SoundManager.LoadContent(Content);
            Round.LoadContent(this);

            ReadSetting();
            HelpMenu = FormGroup.LoadFormGroup(Content, "Content/Xml/Help.xml");
            HelpMenu.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            HelpMenu.ListenEvent = true;
            HelpMenu.Game = this;

            GameOver = FormGroup.LoadFormGroup(Content, "Content/Xml/GameOver.xml");
            GameOver.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            GameOver.ListenEvent = true;
            GameOver.Game = this;

            PrevNextRound = FormGroup.LoadFormGroup(Content, "Content/Xml/NextRound.xml");
            PrevNextRound.ListenEvent = true;
            PrevNextRound.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            PrevNextRound.Game = this;

            Winner = FormGroup.LoadFormGroup(Content, "Content/Xml/Winner.xml");
            Winner.ListenEvent = true;
            Winner.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            Winner.Game = this;

            PlayContinue = FormGroup.LoadFormGroup(Content, "Content/Xml/PlayContinues.xml");
            PlayContinue.ListenEvent = true;
            PlayContinue.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            PlayContinue.Game = this;

            MainMenu = FormGroup.LoadFormGroup(Content, "Content/Xml/MainMenu.xml");
            MainMenu.ListenEvent = true;
            MainMenu.Size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            MainMenu.Game = this;
            OpenMainMenu();
        }

        protected override void UnloadContent()
        {
            SoundManager.UnLoadContent();
            Round.UnloadContent();
        }
        protected override void Update(GameTime gameTime)
        {
            if (Current != null && Current.Activity)
                Current.Update(gameTime);
            else if (MainMenu.Enable)
                MainMenu.Update(gameTime, null);
            else if (HelpMenu.Enable)
            {
                HelpMenu.Update(gameTime, null);
            }
            else if (GameOver.Enable)
            {
                GameOver.Update(gameTime, null);
            }
            else if (PrevNextRound.Enable)
            {
                PrevNextRound.Update(gameTime, null);
            }
            else if (Winner.Enable)
            {
                Winner.Update(gameTime, null);
            }
            else if (PlayContinue.Enable)
            {
                PlayContinue.Update(gameTime, null);
            }
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            if (Current != null && Current.Activity)
            {
                Current.Draw(gameTime, spriteBatch);
            }
            else if (MainMenu.Visible)
            {
                MainMenu.Draw(gameTime, spriteBatch, null);
            }
            else if (HelpMenu.Visible)
            {
                HelpMenu.Draw(gameTime, spriteBatch, null);
            }
            else if (GameOver.Visible)
            {
                GameOver.Draw(gameTime, spriteBatch, null);
            }
            else if (PrevNextRound.Visible)
            {
                PrevNextRound.Draw(gameTime, spriteBatch, null);
            }
            else if (Winner.Visible)
            {
                Winner.Draw(gameTime, spriteBatch, null);
            }
            else if (PlayContinue.Visible)
            {
                PlayContinue.Draw(gameTime, spriteBatch, null);
            }
            base.Draw(gameTime);
            spriteBatch.End();
        }

        public void ReadSetting()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(SettingPath);
            XmlNodeList xmlRounds = doc.SelectNodes("//Rounds/Round");
            foreach (XmlElement xml in xmlRounds)
            {
                String map = xml.GetAttribute("map");
                int score = int.Parse(xml.GetAttribute("score"));
                int index = 1;
                this.Rounds.Add(new RoundInfo
                {
                    Index = index++,
                    Map = map,
                    Score = score
                });
            }
        }

        public void NewGame()
        {
            this.Index = 0;
            MyCharacter hero = Round.Hero;
            if (hero != null)
            {

                hero.Initialize();
                hero.Live = 2;
            }
            OpenPrevNextRound();
        }

        public void OpenNextRound()
        {
            this.Index++;
            OpenPrevNextRound();
        }

        public void Play()
        {
            this.StopAll();
            this.Current = new Round(this);
            this.Current.Activity = true;
            if (this.Index == 0)
                Round.Hero.Score = 0;
        }

        public void StopAll()
        {
            if (this.Current != null)
            {
                Round.SoundBackground.Pause();
                this.Current.Activity = false;
            }
            if (MainMenu != null)
            {
                MainMenu.Enable = MainMenu.Visible = false;
            }
            if (HelpMenu != null)
            {
                HelpMenu.Enable = HelpMenu.Visible = false;
            }
            if (GameOver != null)
            {
                GameOver.Enable = GameOver.Visible = false;
            }
            if (PrevNextRound != null)
            {
                PrevNextRound.Enable = PrevNextRound.Visible = false;
            }
            if (Winner != null)
            {
                Winner.Enable = Winner.Visible = false;
            }
            if (PlayContinue != null)
            {
                PlayContinue.Enable = PlayContinue.Visible = false;
            }
        }

        public void OpenGameOver()
        {
            StopAll();
            GameOver.Enable = GameOver.Visible = true;
        }

        public void OpenHelp()
        {
            StopAll();
            this.HelpMenu.Visible = this.HelpMenu.Enable = true;
        }

        public void LoadGame()
        {
        }

        public void SaveGame()
        {

        }

        public void OpenMainMenu()
        {
            StopAll();
            this.MainMenu.Visible = this.MainMenu.Enable = true;
        }

        public void OpenPrevNextRound()
        {
            StopAll();
            if (this.Index < this.Rounds.Count)
            {
                MainGame.CurrentRoundInfo = this.Rounds[Index];
                this.PrevNextRound.Enable = this.PrevNextRound.Visible = true;
            }
            else
            {
                OpenWinner();
            }
        }
        public void OpenWinner()
        {
            StopAll();
            Winner.Enable = Winner.Visible = true;
        }

        public void OpenContinue()
        {
            StopAll();
            PlayContinue.Enable = PlayContinue.Visible = true;
        }
    }
}
